package com.exam.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.exam.bean.User;
import com.exam.dao.UserDao;
import com.exam.hibernate.HibernateSessionFactory;
import com.exam.util.Page;

public class UserDaoImpl implements UserDao {

	public boolean add(User obj) throws Exception {
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();// 获得Session对象
		Transaction transaction = null;// 声明一个事务对象
		try {
			transaction = session.beginTransaction();// 开启事务
			session.save(obj);// 保存信息
			transaction.commit();// 提交事务
			flag = true;
		} catch (Exception ex) {
			transaction.rollback();// 事务回滚
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session对象
		}
		return flag;
	}

	public boolean delete(Integer id) throws Exception {
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();// 获得Session对象
		Transaction transaction = null;// 声明一个事务对象
		try {
			transaction = session.beginTransaction();// 开启事务
			Query query = session.createQuery("delete from User where id=?");// 删除信息
			query.setParameter(0, id);// 设置条件参数
			query.executeUpdate();// 执行语句
			transaction.commit();// 提交事务
			flag = true;
		} catch (Exception ex) {
			transaction.rollback();// 事务回滚
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session对象
		}
		return flag;
	}

	public boolean delete(List<Integer> idList) throws Exception {
		boolean flag = false;
		StringBuilder sbParam = new StringBuilder();
		if (idList != null && idList.size() > 0) {
			int size = idList.size();
			sbParam.append("(");
			for (int i = 0; i < size; i++) {
				if (i < size - 1) {
					sbParam.append(idList.get(i) + ",");
				} else {
					sbParam.append(idList.get(i) + ")");
				}
			}
			Session session = HibernateSessionFactory.getSession();// 获得Session对象
			Transaction transaction = null;// 声明一个事务对象
			try {
				transaction = session.beginTransaction();// 开启事务
				String hql = "delete from User where id in "
						+ sbParam.toString();// 注意from后面的名字(User)需要和实体类名称一致
				Query query = session.createQuery(hql);// 删除信息
				query.executeUpdate();// 执行语句
				transaction.commit();// 提交事务
				flag = true;
			} catch (Exception ex) {
				transaction.rollback();// 事务回滚
				throw ex;
			} finally {
				HibernateSessionFactory.closeSession();// 关闭Session对象
			}
		}
		return flag;
	}

	public boolean delete(String strWhere) throws Exception {
		boolean flag = false;
		if (strWhere != null && !"".equals(strWhere)) {
			StringBuilder sbHql = new StringBuilder();
			sbHql.append("delete from User ");// 注意from后面的名字(User)需要和实体类名称一致
			sbHql.append("where ").append(strWhere);

			Session session = HibernateSessionFactory.getSession();// 获得Session对象
			Transaction transaction = null;// 声明一个事务对象
			try {
				transaction = session.beginTransaction();// 开启事务
				Query query = session.createQuery(sbHql.toString());// 删除信息
				query.executeUpdate();// 执行语句
				transaction.commit();// 提交事务
				flag = true;
			} catch (Exception ex) {
				transaction.rollback();// 事务回滚
				throw ex;
			} finally {
				HibernateSessionFactory.closeSession();// 关闭Session对象
			}
		}
		return flag;
	}

	@SuppressWarnings("unchecked")
	public List<User> findListByPage(Page page) throws Exception {
		List<User> list = null;
		Session session = HibernateSessionFactory.getSession();// 创建一个Session对象
		try {
			Query query = session
					.createQuery("from User order by u_createDateTime desc");
			query.setMaxResults(page.getEveryPage());// 设置查询记录数
			query.setFirstResult(page.getBeginIndex());// 设置查询记录起始位置
			list = query.list(); // 查询结果保存到list中
		} catch (Exception ex) {
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<User> findListByPage(String strWhere, Page page)
			throws Exception {
		List<User> list = null;
		StringBuilder sbHql = new StringBuilder();
		sbHql.append("from User ");
		if (strWhere != null && !"".equals(strWhere)) {
			sbHql.append("where ").append(strWhere);
		}
		sbHql.append(" order by createDateTime desc");
		Session session = HibernateSessionFactory.getSession();// 创建一个Session对象
		try {
			Query query = session.createQuery(sbHql.toString());
			query.setMaxResults(page.getEveryPage());// 设置查询记录数
			query.setFirstResult(page.getBeginIndex());// 设置查询记录起始位置
			list = query.list(); // 查询结果保存到list中
		} catch (Exception ex) {
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public int findCount(String strWhere) throws Exception {
		int count = 0;
		Session session = HibernateSessionFactory.getSession();// 创建一个Session对象
		try {
			Criteria criteria = session.createCriteria(User.class);// 指定针对的实体
			if (strWhere != null && !"".equals(strWhere)) {
				criteria.add(Restrictions.sqlRestriction(strWhere));// 添加条件
			}
			criteria.setProjection(Projections.rowCount());// 结果集得到的记录数
			List<Integer> results = criteria.list();// 取得结果
			count = results.get(0);// 取得记录数
		} catch (Exception ex) {
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return count;
	}

	@SuppressWarnings("unchecked")
	public List<User> findList(String strWhere) throws Exception {
		List<User> list = null;
		StringBuilder sbHql = new StringBuilder();
		sbHql.append("from User ");
		if (strWhere != null && !"".equals(strWhere)) {
			sbHql.append("where ").append(strWhere);
		}
		sbHql.append(" order by createDateTime desc");
		Session session = HibernateSessionFactory.getSession();// 创建一个Session对象
		try {
			Query query = session.createQuery(sbHql.toString());// 查找信息
			list = query.list();// 取得的数据列表
		} catch (Exception ex) {
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return list;
	}

	public User findModel(Integer id) throws Exception {
		User user = null;
		Session session = HibernateSessionFactory.getSession();// 创建一个Session对象
		try {
			Query query = session.createQuery("from User where id=?");
			query.setParameter(0, id);// 设置参数
			user = (User) query.list().get(0);
		} catch (Exception ex) {
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return user;
	}

	public boolean update(User obj) throws Exception {
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();// 获得session对象
		Transaction transaction = null;// 声明一个事务对象
		try {
			transaction = session.beginTransaction();// 开启一个事务
			session.update(obj);// 执行更新
			transaction.commit();// 提交事务
			flag = true;
		} catch (Exception ex) {
			transaction.rollback();// 回滚事务
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return flag;
	}

	public boolean update(List<User> objList) throws Exception {
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();// 获得session对象
		Transaction transaction = null;// 声明一个事务对象
		try {
			transaction = session.beginTransaction();// 开启一个事务
			for (User user : objList) {
				session.update(user);// 更新操作
			}
			transaction.commit();// 提交事务
			flag = true;
		} catch (Exception ex) {
			transaction.rollback();// 回滚事务
			throw ex;
		} finally {
			HibernateSessionFactory.closeSession();// 关闭Session
		}
		return flag;
	}
}
